package cn.ilikes.tools.hbase.jpa.metadatas.processor.validators;

import javax.persistence.Entity;
import javax.persistence.Table;

import org.slf4j.Logger;

import cn.ilikes.tools.hbase.jpa.metadatas.IEntityMetaData;
import cn.ilikes.tools.logs.Loggerfactory;

public class EntityTableAndEntityValidator extends AbEntityValidator implements EntityValidator {
	private Logger logger = Loggerfactory.instance(EntityTableAndEntityValidator.class);

	@Override
	public boolean entityValidator(Class<?> clazz, IEntityMetaData entityMetaData) throws EntityValidatorException {
		ValidatorTable(clazz, entityMetaData);
		ValidatorEntity(clazz, entityMetaData);
		return true;
	}

	/**
	 * @Table(name = "ilikes_user_ilikes", schema =
	 *             HbaseIndex.ILIKES_SCHEMA)
	 * @param clazz
	 * @throws EntityValidatorException
	 */
	private void ValidatorTable(Class<?> clazz, IEntityMetaData entityMetaData) throws EntityValidatorException {
		if (!clazz.isAnnotationPresent(Table.class))
			throwEntityValidatorException(logger, "this entity not find  @Table ");
		Table table = clazz.getAnnotation(Table.class);
		if (validatorisEmpty(table.name()))
			throwEntityValidatorException(logger, "this entity not find  @Table  name not find ");
		if (validatorisEmpty(table.schema()))
			throwEntityValidatorException(logger, "this entity not find  @Table  schema  not find ");
		if ((!table.schema().equals(entityMetaData.getPuName())))
			throwEntityValidatorException(logger, "this entity not find  @Table  schema   not  contain  @  ; eg : tableName@puName");

	}

	/**
	 * @Table(name = "ilikes_user_ilikes", schema =
	 *             HbaseIndex.ILIKES_SCHEMA)
	 * @param clazz
	 * @throws EntityValidatorException
	 */
	private void ValidatorEntity(Class<?> clazz, IEntityMetaData entityMetaData) throws EntityValidatorException {
		if (!clazz.isAnnotationPresent(Entity.class))
			throwEntityValidatorException(logger, "this entity not find  @Entity ");

	}

}
